import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:full_display/common/qxp_color.dart';
import 'package:full_display/common/qxp_style.dart';
import 'package:full_display/generated/l10n.dart';
import 'package:full_display/provide/cmd_provider.dart';
import 'package:full_display/provide/time_provider.dart';
import 'package:full_display/unit/screen/screen_utility.dart';
import 'package:full_display/unit/web_socket/web_sockect_utils.dart';
import 'package:full_display/unit/widgets/ckeckbox/widget_checkbox.dart';
import 'package:provider/provider.dart';

class AddClockDialog extends Dialog {
  AddClockDialog({Key? key});
  late TimeProvider _TimeData;
  late CMDProvider _dataData;
  String startTime = '';
  String endTime = '';

  void _showTimePicker(context, String state) {
    showCupertinoModalPopup(
      context: context,
      builder: (context) {
        final cupertinoTimerPicker = CupertinoTimerPicker(
          mode: CupertinoTimerPickerMode.hm, // 时间选择器模式
          initialTimerDuration: state == 'start'
              ? const Duration(hours: 08, minutes: 0)
              : const Duration(hours: 20, minutes: 0), // 初始化时间
          minuteInterval: 1, // 分钟间隔
          alignment: Alignment.center, // 对齐方式
          backgroundColor: Colors.white, // 背景色
          // 选中时间变化回调
          onTimerDurationChanged: (dur) {
            if (state == 'start') {
              _TimeData.setStartTime(
                  "${dur.inHours > 9 ? dur.inHours : "0${dur.inHours}"}:${(dur.inMinutes - dur.inHours * 60) > 9 ? (dur.inMinutes - dur.inHours * 60) : "0${(dur.inMinutes - dur.inHours * 60)}"}");
            } else {
              _TimeData.setEndTime(
                  "${dur.inHours > 9 ? dur.inHours : "0${dur.inHours}"}:${(dur.inMinutes - dur.inHours * 60) > 9 ? (dur.inMinutes - dur.inHours * 60) : "0${(dur.inMinutes - dur.inHours * 60)}"}");
            }
          },
        );
        return AnimatedContainer(
          duration: Duration(microseconds: 200),
          height: 268,
          width: double.infinity,
          child: Container(
            color: Colors.white,
            child: Column(
              children: [
                Expanded(child: cupertinoTimerPicker),
              ],
            ),
          ),
        );
      },
    );
  }

  @override
  Widget build(BuildContext context) {
    _TimeData = context.watch<TimeProvider>();
    _dataData = context.watch<CMDProvider>();
    return Material(
      type: MaterialType.transparency,
      child: Center(
        child: Container(
          width: 320.0.W,
          height: 638.0.W,
          decoration: const BoxDecoration(
            gradient: LinearGradient(
                //渐变位置
                begin: Alignment.topCenter, //右上
                end: Alignment.bottomCenter, //左下
                //渐变颜色[始点颜色, 结束颜色]
                colors: [ColorPlate.app_blue, ColorPlate.sub_blue]),
            borderRadius: BorderRadius.all(Radius.circular(8.0)),
          ),
          child: Column(
            children: [
              Container(
                padding: EdgeInsets.only(left: 12.0.W, right: 12.0.W),
                child: Column(
                  children: [
                    Container(
                      padding: EdgeInsets.only(top: 20.0.W),
                      child: Text(S.of(context).Add_timing,
                          style: StandardTextStyle.textWhite17BoldStyle),
                    ),
                    InkWell(
                      onTap: () {
                        _showTimePicker(context, 'start');
                      },
                      child: Container(
                        padding: EdgeInsets.only(top: 30.0.W, bottom: 5.0.W),
                        child: Row(
                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                          children: [
                            Text(S.of(context).The_start_time,
                                textAlign: TextAlign.left,
                                style: StandardTextStyle.textWhite17Style),
                            Text('${_TimeData.listTime[0]}',
                                textAlign: TextAlign.left,
                                style: StandardTextStyle.textWhite17Style),
                          ],
                        ),
                      ),
                    ),
                    Divider(
                      height: 1.0.W,
                      color: ColorPlate.white, // 分割线颜色
                    ),
                    InkWell(
                      onTap: () {
                        _showTimePicker(context, 'end');
                      },
                      child: Container(
                        padding: EdgeInsets.only(top: 30.0.W, bottom: 5.0.W),
                        child: Row(
                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                          children: [
                            Text(S.of(context).The_end_of_time,
                                textAlign: TextAlign.left,
                                style: StandardTextStyle.textWhite17Style),
                            Text('${_TimeData.listTime[1]}',
                                textAlign: TextAlign.left,
                                style: StandardTextStyle.textWhite17Style),
                          ],
                        ),
                      ),
                    ),
                    Divider(
                      height: 1.0.W,
                      color: ColorPlate.white, // 分割线颜色
                    ),
                    CheckBoxWidget(listC: _TimeData.listWeek),
                  ],
                ),
              ),
              Container(
                padding: EdgeInsets.only(top: 20.0.W),
                child: Divider(
                  height: 1.0.W,
                  color: ColorPlate.white, // 分割线颜色
                ),
              ),
              Expanded(
                child: InkWell(
                  onTap: () {
                    int week = 0;
                    _TimeData.listWeek.forEach((element) {
                      if (element['state'] == true) {
                        week = week | (1 << (element['value'] - 1));
                      }
                    });

                    //转换成对应日期
                    // for (var k = 0; k < 7; k++) {
                    //   if ((week & (1 << k)) != 0) {
                    //     var dataWeek = {week: k + 1};
                    //   }
                    // }
                    int index = 1;
                    List data = [];
                    print("data========>${_dataData.deviceInfo}");
                    if (_dataData.deviceInfo.isNotEmpty) {
                      if (_dataData.deviceInfo['properties']['time'] != null) {
                        for (var i = 0;
                            i <
                                _dataData
                                    .deviceInfo['properties']['time'].length;
                            i++) {
                          var dic = {
                            "id": index, //
                            "starttime":
                                "2020-12-19T${_dataData.deviceInfo['properties']['time'][i]['starttime']}:00.332+00:00", //开始时间
                            "endtime":
                                "2020-12-19T${_dataData.deviceInfo['properties']['time'][i]['endtime']}:30.332+00:00", //结束时间
                            "devid": 0, //设备ID
                            "mergeid": 0, //设备ID
                            "houseid": 0, //设备ID
                            "devsandweek": _dataData.deviceInfo['properties']
                                ['time'][i]['devsandweek']
                          };
                          data.add(dic);
                          index++;
                        }
                      }
                    }
                    var dic = {
                      "id": index, //
                      "starttime":
                          "2020-12-19T${_TimeData.listTime[0]}:00.332+00:00", //开始时间
                      "endtime":
                          "2020-12-19T${_TimeData.listTime[1]}:30.332+00:00", //结束时间
                      "devid": 0, //设备ID
                      "mergeid": 0, //设备ID
                      "houseid": 0, //设备ID
                      "devsandweek": week
                    };
                    data.add(dic);

                    WebSocketUtils().sendCustomMessage(53, content: data);
                    Navigator.pop(context);
                  },
                  child: Center(
                    child: Text(S.of(context).Add_timing,
                        style: StandardTextStyle.textWhite17BoldStyle),
                  ),
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}
